'use strict';

angular
  .module('cmsysApp')
  .service('projectService',
    [
      '$http',
      'baseService',
      'pageService',
      function ($http, baseService, pageService) {
        var self = this;
        this.findAllTypes = function ($scope) {
          baseService.query($scope.operations.findAllTypes).then(function (data) {
            $scope.states = data.states;
            $scope.companies = data.companies;
          });
        };
        this.projectList = function ($scope) {
          self.params = $scope.params || {};
          pageService.query($scope.params).then(function (page) {
            pageService.loadData($scope, page);
          });
        };
        this.del = function (del) {
          baseService.del(del)
        };
        this.edit = function (edit) {
          baseService.edit(edit)
        };
      }])
  .controller(
    'projectCtrl',
    [
      '$scope',
      '$http',
      '$state',
      '$stateParams',
      'projectService',
      'pageService',
      '$filter',
      'userPrivileges',
      function ($scope, $http, $state, $stateParams, projectService, pageService, $filter, userPrivileges) {
        $scope.opt = userPrivileges['删除项目'];
        $scope.status = userPrivileges['修改项目'];
        $scope.params = {
          'url': baseUrl + '/project/projectList',
          'conditions': {
            'condition': '',
            'currentPage': 1,
            'pageSize': 10,
          }
        };
        $scope.operations = {
          'del': {
            'url': baseUrl + '/project/delProject',
            'params': $scope.project
          },
          'edit': {
            'url': baseUrl + '/project/editProject',
            'params': $scope.project
          },
          'findAllTypes': {
            'url': baseUrl + '/project/findAllTypes'
          }
        }
        $scope.go = function (pageNum) {
          $scope.params.conditions.currentPage = pageNum;
          projectService.projectList($scope);
        }
        $('#textarea').wysihtml5();
        $scope.changePS = function () {
          pageService.changePS($scope);
          projectService.projectList($scope);
        };
        $scope.changeCD = function () {
          pageService.changeCD($scope);
          projectService.projectList($scope);
        };

        projectService.findAllTypes($scope);
        projectService.projectList($scope);
        $scope.edit = function (pid, sid, e) {
          $scope.stopPropagation(e);
          $scope.operations.edit.params = {'pid': pid, 'pstate': sid};
          projectService.edit($scope.operations.edit);
          projectService.projectList($scope);
        };
        $scope.stopPropagation = function (e) {
          if (e && e.stopPropagation) {
            //因此它支持W3C的stopPropagation()方法
            e.stopPropagation();
          } else {
            //否则我们使用ie的方法来取消事件冒泡
            window.event.cancelBubble = true;
          }
        }

        $scope.toggle = function (e) {
          $scope.stopPropagation(e);
          var $ele = $(e.target), $parentDiv = $ele.parent();
          if ($parentDiv.hasClass('open')) {
            $parentDiv.removeClass('open');
            $ele.attr('aria-expanded', 'true');
          }
          else {
            $ele.attr('aria-expanded', 'false');
            $parentDiv.addClass('open');
          }
          //$('#toggle').attr('aria-expanded', 'true');
        }
        $scope.del = function (pid, e) {
          $scope.stopPropagation(e);
          $scope.operations.del.params = {pid: pid};
          projectService.del($scope.operations.del);
          projectService.projectList($scope);
        }
        $scope.addProject = function () {
          $('#project').modal('toggle');
        };

        $scope.save = function () {
          $('#project').modal('toggle');
          $scope.project['detail'] = $(".textarea").val();
          $scope.operations.edit.params = $scope.project;
          $scope.project.biddingtime = $filter('date')($scope.project.biddingtime1, "yyyy/MM/dd");
          $scope.project.begintime = $filter('date')($scope.project.begintime1, "yyyy/MM/dd");
          $scope.project.endtime = $filter('date')($scope.project.endtime1, "yyyy/MM/dd");
          projectService.edit($scope.operations.edit);
          projectService.projectList($scope);
          $scope.project = {};
          $scope.myForm.$setPristine();
        }
        $scope.changeState = function (sid) {
          if (sid >= 0)
            $scope.params.conditions.state = sid;
          else
            delete $scope.params.conditions.state;
          projectService.projectList($scope);
        }
        $scope.cancel = function () {
          $scope.project = {};
          $scope.myForm.$setPristine();
          $('#project').modal('toggle');
        }
        $scope.projectInfo = function (index) {
          $scope.pjtInfo = $scope.page.recordList[index];
          $scope.pjtInfo.bidding = $scope.pjtInfo.pstate >= 2;
          $scope.pjtInfo.begin = $scope.pjtInfo.pstate >= 3;
          $scope.pjtInfo.end = $scope.pjtInfo.pstate >= 4;
          var prepara = new Date($scope.pjtInfo.biddingtime);
          prepara.setDate(new Date($scope.pjtInfo.biddingtime).getDate() - 15);
          prepara.setMonth(prepara.getMonth());
          $scope.pjtInfo.preparationdate = $filter('date')(prepara, "yyyy/MM/dd");
          $('#projectInfo').modal('toggle');
        }
      }
    ])
;
